<template>
	<view class="control" :style="{paddingBottom:selectionArr.length > 1 ?'400rpx' : '200rpx'}">
		<view class="time-list">
			<view @click="selectDay(item,index)" :class="{'active':dayActive == index}" class="time-item"
				v-for="(item,index) in dataList" :key="index">
				<view class="day">{{item.weeks}}</view>
				<view class="date">{{item.mouth}}/{{item.day}}</view>
			</view>
		</view>
		<u-notice-bar fontSize="12" direction="column" :text="['节假日期间预定场地不得退订，若要退订请联系客服']"></u-notice-bar>
		<view v-if="panel" style="width: 100%; height: 100vh;background-color:#000;
      opacity: 0.4; 
      position: absolute;
      top:0;
      left:0;z-index: 999;">

		</view>
		<!-- 场地预定表格 -->
		<view class="table-box">
			<view class="tr colum">
				<view class="td date" v-for="(item,index) in fieldlist" :key="index">
					<view class="date-1" v-if="index !== 0">{{item.starttime}}</view>
					<view v-if="index !== 0">{{item.endtime}}</view>
				</view>
			</view>
			<scroll-view class="table" scroll-x>
				<view class="tr">
					<view class="td name" v-for="(item,index) in fieldNoList">
						<view>{{item}}</view>
					</view>
				</view>
				<view class="tr" v-for="(line,index) in fieldlist" :key="index">
					<!-- <view :class="{'select-td':selectList.includes(indexs)}" class="td" @click="selectData(colum,indexs)" -->
					<!-- <view :class="colum.time_price != '' ? 'select-blue' : 'select-grey'" class="td"
						v-for="(colum,indexs) in line.fieldlist_s" :key="indexs"> -->
					<view class="td" v-for="(colum,indexs) in line.fieldlist_s" :key="indexs">
						<!-- 内容 -->
						<!-- <view class="price-box" @click="selectData(colum,indexs,line, 1,fieldlist)"
							v-if="colum.background=='#ffffff'||!colum.background">
							<view class="price" v-if="colum.time_price != ''">
								<view style="width: 100rpx; height: 80rpx;background: #59A0FFFF;border-radius: 8px;display: flex;align-items: center;justify-content: center;">
									<view style="padding-left: 2rpx;">{{colum.timeprice}}</view>
								</view>
							</view>
						</view>
						<view class="price-box" @click="selectData(colum,indexs,line, 2)" v-else>
							<view class="price pr" v-if="colum.time_price != ''">
								<view class="pr" style="width: 100rpx; height: 80rpx; background: #DF42D9;border-radius: 8px;display: flex;align-items: center;justify-content: center;">
									<view style="padding-left: 2rpx;">{{colum.timeprice}}</view>
								</view>
							</view>
						</view> -->
						<view class="price-box" @click="selectData(colum,line, 1,index,indexs)">
							<view class="price" v-if="colum.time_price != ''">
								<view
									:class="!able_list && line.endtime > time_list ? 'select-blue' :  colum.text == able_list && line.endtime > time_list ? 'select-blue' : 'select-grey'"
									style="width: 100rpx; height: 80rpx;display: flex;align-items: center;justify-content: center;background-size: 100% 100%;">
									<view v-show="!colum.select" style="padding-left: 2rpx;">{{colum.timeprice}}</view>
								</view>
							</view>
						</view>


					</view>
				</view>
			</scroll-view>
		</view>

		<view class="footer">
			<scroll-view class="footer-3" scroll-x v-if="selectionArr.length > 0">
				<view class="flex-align">
					<view class="item" v-for="(im,indexx) in selectionArr" :key="indexx">
						<view class="icon" @click="removeData(im,indexx)">
							<u-icon name="close-circle-fill" color="#000000" size="17"></u-icon>
						</view>
						<view class="item-time">{{im.month_day}}({{im.week}})</view>
						<view class="item-con">
							<view class="item-con1">羽毛球-{{im.num}}</view>
							<view class="item-con1">{{im.dateTimes}}</view>
						</view>
					</view>
				</view>

			</scroll-view>
			<view class="footer-2 flex-align" v-if="selectionArr.length === 0">
				<view class="info-1 flex-align">
					<image mode="aspectFit" :src="buju_img"></image>
					<view>场地布局图</view>
				</view>
				<view class="info-2 flex-between">
					<view class="tag-item flex-align">
						<view class="color color1"></view>
						<view>已售</view>
					</view>
					<view class="tag-item flex-align">
						<view class="color color2"></view>
						<view class="color color3"></view>
						<view>可选</view>
					</view>
					<view class="tag-item flex-align">
						<view class="color color4"></view>
						<view>已选</view>
					</view>
				</view>
			</view>
			<view class="footer-1 flex-between">
				<view class="flex-align" v-if="selectionArr.length > 0">
					<view class="info-1">总计：</view>
					<view class="info-2">
						￥<text>{{money}}</text>
					</view>
				</view>

				<view class="flex-align" v-if="selectionArr.length === 0">
					<view class="info-1">总计：</view>
					<view class="info-2">
						￥<text>00.00</text>
					</view>
				</view>

				<view class="btn_n" v-if="selectionArr.length > 0" @click="reserved_site">预定场地</view>
				<view class="btn" v-else>预定场地</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				buju_img: this.$https.assetsPath + "image13.png",
				panel: true,
				maxbooknum: 3, // 一次订多少个场地
				maxbooknum_s: 2, // 一次订多少个人
				maxbooknum_as: 4, // 一次订多少个团
				days: 4, // 选择天数
				data_time: true,
				timne_s: '',
				dayActive: 0,
				dataList: [], //日期
				selectList: [],
				fieldNoList: ["1号场", "2号场", "3号场", "4号场", "5号场", "6号场", "7号场"],
				able_list: null,
				able_s: null,
				time_list: '',
				time_a: null,
				fieldlist: [
					// {
					// 	id: "14",
					// 	starttime: "",
					// 	endtime: "",
					// 	fieldlist_s: []
					// },
					{
						id: "37",
						starttime: "08:00",
						endtime: "10:00",
						fieldlist_s: [{
								timeprice: "20元/团 1/2团",
								time_price: "20",
								text: 1,
								select: false

							},
							{
								timeprice: "5元/人 2/5人",
								time_price: "5",
								text: 2,
								select: false,
							},
							{
								timeprice: "30元/团 1/2团",
								time_price: "30",
								text: 1,
								select: false
							},
							{
								timeprice: "",
								time_price: "",
								text: 0,
								select: false
							},

							{
								timeprice: "30元/场",
								time_price: "30",
								text: 3,
								select: false
							},
							{
								timeprice: "",
								time_price: "",
								text: 0,
								select: false
							},
							{
								timeprice: "8元/人  2/5人",
								time_price: "8",
								text: 2,
								select: false
							},
						]
					},
					{
						id: "13",
						starttime: "10:00",
						endtime: "17.00",
						fieldlist_s: [{
								timeprice: "60元",
								time_price: "60",
								text: 1,
								select: false
							},
							{
								timeprice: "80元",
								time_price: "80",
								text: 1,
								select: false
							},
							{
								timeprice: "",
								time_price: "",
								text: 0,
								select: false
							},
							{
								timeprice: "6元",
								time_price: "6",
								text:1,
								select: false
							},
							{
								timeprice: "40元",
								time_price: "4",
								text: 1,
								select: false
							},
							{
								timeprice: "40元",
								time_price: "40",
								text: 1,
								select: false
							},
							{
								timeprice: "",
								time_price: "",
								text: 0,
								select: false
							}
						]
					},
					{
						id: "28",
						starttime: "17:00",
						endtime: "22:00",
						fieldlist_s: [{
								timeprice: "90元",
								time_price: "90",
								text: 1,
								select: false
							},
							{
								timeprice: "70元",
								time_price: "70",
								text: 1,
								select: false
							},
							{
								timeprice: "",
								time_price: "",
								text: 0,
								select: false
							},
							{
								timeprice: "7元",
								time_price: "7",
								text: 1,
								select: false
							},
							{
								timeprice: "50元",
								time_price: "50",
								text: 1,
								select: false
							},
							{
								timeprice: "",
								time_price: "",
								text: 1,
								select: false
							},
							{
								timeprice: "7元",
								time_price: "7",
								text: 1,
								select: false
							}
						]
					}
				],
				selectionArr: [], // 选中的场地
				current_year_month: '', //年月日
				current_month_day: '', //几月几号
				today: '', //日期
				line_time: '', //时间戳
				weeks_data: '', //星期几
				money: 0, //总计价格
				images: this.$https.assetsPath + "image13.png",
				colum: {},
				timne_t: '',
				item_time: '',
				it: ''
			}
		},
		created() {
			this.getDataList()
		},
		onLoad(options) {
			var _this = this
			_this.getCurrentMonthDay()
			let fieldlist = this.fieldlist
			fieldlist.unshift({
				id: "14",
				starttime: "",
				endtime: "",
				fieldlist_s: [],
			})
			let fieldNoList = this.fieldNoList
			fieldNoList.push('11')
			if (this.time_list > '08:00' && this.time_list < '24:00') {
				this.panel = false
			}
		},
		onShow() {

		},
		methods: {
			// 获取今天当前日期
			getCurrentMonthDay() {
				var _this = this
				var now = new Date()
				var y = now.getFullYear()
				var m = now.getMonth() + 1
				var d = now.getDate()
				var h = now.getHours(); //获取当前小时数(0-23)
				var min = now.getMinutes(); //获取当前分钟数(0-59)
				if (m < 10) {
					m = '0' + m
				}
				if (d < 10) {
					d = '0' + d
				}
				if (h < 10) {
					h = '0' + h
				}
				if (min < 10) {
					min = '0' + min
				}
				var date = m + '/' + d
				var date_s = m + '-' + d
				_this.current_month_day = date_s
				_this.current_year_month = y + '-' + m + '-' + d
				_this.today = date
				_this.weeks_data = '今天'
				this.time_list = h + ':' + min
				console.log(this.time_list)

			},
			// 选择每天日期
			selectDay(item, index) {
				var _this = this
				_this.dayActive = index
				var now = new Date()
				var y = now.getFullYear()
				_this.current_year_month = y + '-' + item.mouth + '-' + item.day
				_this.current_month_day = item.mouth + '-' + item.day
				_this.current_month_day = item.mouth + '-' + item.day
				_this.weeks_data = item.weeks
				// console.log(_this.weeks_data)
				// console.log(_this.current_month_day)
				// console.log(_this.current_year_month)
			},
			// 选择具体场地
			selectData(colum, line, type, index, indexs) {
				let _this = this
				// if (this.today) {
				// console.log(line.endtime)
				// console.log(this.time_list)
				if (line.endtime <= this.time_list) {
					uni.showToast({
						title: '已经过了时间段',
						icon: 'none', //如果要纯文本，不要icon，将值设为'none'
						duration: 2000 //持续时间为 2秒
					})
					return
				}
				// } 

				if (this.able_list && colum.text != this.able_list) {
					uni.showToast({
						title: '不能选择不同类型的场次',
						icon: 'none', //如果要纯文本，不要icon，将值设为'none'
						duration: 2000 //持续时间为 2秒
					})
					return
				}
				// if (colum.text === 3) {
				// 	if (!colum.select && _this.selectionArr.length == _this.maxbooknum) {
				// 		uni.showToast({
				// 			title: '您所订场地数量超过限制',
				// 			icon: 'none', //如果要纯文本，不要icon，将值设为'none'
				// 			duration: 2000 //持续时间为 2秒
				// 		})
				// 		return
				// 	}
				// }
				// if (colum.text === 2) {
				// 	if (!colum.select && _this.selectionArr.length == _this.maxbooknum_s) {
				// 		uni.showToast({
				// 			title: '您所订场地数量超过限制',
				// 			icon: 'none', //如果要纯文本，不要icon，将值设为'none'
				// 			duration: 2000 //持续时间为 2秒
				// 		})
				// 		return
				// 	}
				// }
				// if (colum.text === 1) {
				// 	if (!colum.select && _this.selectionArr.length == _this.maxbooknum_as) {
				// 		uni.showToast({
				// 			title: '您所订场地数量超过限制',
				// 			icon: 'none', //如果要纯文本，不要icon，将值设为'none'
				// 			duration: 2000 //持续时间为 2秒
				// 		})
				// 		return
				// 	}
				// }

				// if (!colum.select && _this.selectionArr.length == _this.maxbooknum) {
				// 	 uni.showToast({
				// 		title: '您所订场地数量超过限制',
				// 		icon: 'none', //如果要纯文本，不要icon，将值设为'none'
				// 		duration: 2000 //持续时间为 2秒
				// 	})
				// 	return
				// }
				this.able_list = colum.text
				colum.select = !colum.select
				if (colum.select) {
					// 买票限制
					// 背景色切换
					// _this.$set(colum, 'backgroundImage', this.images)
					_this.$set(colum, 'background', '#ffff7f')

					// 获取 X Y 坐标上的时间戳和场地
					let line_time = line.starttime + "-" + line.endtime
					_this.line_time = line_time
					_this.fieldNoList.forEach((im, idx) => {
						if (indexs === idx) {
							_this.timne_s = im
						}
					})
					// console.log(colum.text,'5544')
					_this.selectionArr.push({
						'num': this.timne_s,
						'dateTimes': line_time,
						'current_month': _this.current_year_month,
						'price': colum.time_price,
						'month_day': _this.current_month_day,
						'week': _this.weeks_data,
						'text': colum.text,
						'backgroundImage': _this.$set(colum, 'backgroundImage', this.images),
						'pIndex': index,
						'sIndex': indexs
					})
					// 总价计算
					_this.money = _this.money + parseFloat(colum.time_price)
				} else {
					// _this.$set(colum, 'backgroundImage', '')
					_this.$set(colum, 'background', '#aaff7f')
					// 取消
					let line_time = line.starttime + "-" + line.endtime
					_this.line_time = line_time
					_this.fieldNoList.forEach((im, idx) => {
						if (indexs === idx) {
							_this.timne_s = im
						}
					})
					// 背景色切换
					// _this.$set(colum, 'background', '#ffffff')
					// 删除下面小标签
					for (var i = 0; i < _this.selectionArr.length; i++) {
						if (_this.selectionArr[i].num == _this.timne_s && _this.selectionArr[i].dateTimes == _this
							.line_time) {
							_this.selectionArr.splice(i, 1)
						}
					}
					// 总价计算
					_this.money = _this.money - parseFloat(colum.time_price)
					if (_this.selectionArr.length == 0) {
						_this.able_list = null
					}
				}

				this.fieldlist = JSON.parse(JSON.stringify(this.fieldlist))
			},
			// 删除
			removeData(im, indexx) {
				let _this = this
				const pItem = this.fieldlist[im.pIndex]
				const item = pItem.fieldlist_s[im.sIndex]
				this.selectData(item, pItem, 1, im.pIndex, im.sIndex)
			},
			// 每天周期
			getDataList() {
				let day = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
				let dataList = []
				for (let i = 0; i < this.days; i++) {
					let date = new Date()
					let nextData = date.setDate(date.getDate() + i)
					let Date2 = new Date(nextData)
					let obj = {
						mouth: (Date2.getMonth() + 1).toString().padStart(2, '0'),
						day: Date2.getDate().toString().padStart(2, '0'),
						weeks: day[Date2.getDay()]
					}
					console.log(obj,'pppp')
					if (i == 0) {
						obj.weeks = '今天'
					}
					dataList.push(obj)
					this.dataList = dataList
					console.log(this.dataList, '1010')
				}
			},
			reserved_site() {
				uni.navigateTo({
					url: `./stadium_details?selectionArr=${encodeURIComponent(JSON.stringify(this.selectionArr))}`,
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.control {
		width: 100%;
		background-color: #F7F7F7;
		min-height: 100vh;
		position: relative;
		padding-bottom: 200rpx;

		.time-list {
			padding: 8rpx 30rpx;
			display: flex;
			align-items: center;

			.time-item {
				flex: 1;
				padding: 15rpx 0;
				border-radius: 10rpx;
				color: #333333;
				display: flex;
				flex-direction: column;
				align-items: center;

				.day {
					font-size: 22rpx;
					font-weight: bold;
				}

				.date {
					font-family: PingFang SC;
					// font-weight: bold;
					font-size: 19rpx;
					margin-top: 10rpx;
				}
			}

			.active {
				background-color: #0E9698;
				color: white;
			}
		}

		.table-box {
			display: flex;
			background-color: white;
		}

		.colum {
			display: block !important;
		}

		.tr {
			display: flex;
			margin-top: 4rpx;

			.td {
				width: 100rpx;
				height: 80rpx;
				margin-right: 8rpx;
				margin-top: 4rpx;
				flex-shrink: 0;
				background: #EAEAEA;
				// border-radius: 8px;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;

				.price-box {
					display: flex;
					flex-direction: row;
					justify-content: center;
					align-items: center;

					.price {
						font-size: 22rpx;
						font-weight: 400;
						color: #FFFFFF;
						// line-height: 28rpx;
						text-align: center;

						text {
							font-size: 20rpx;
						}
					}

					.price-2 {
						font-size: 20rpx;
						font-weight: 400;
						color: #FFFFFF;
					}
				}
			}

			.date {
				color: #333333;
				font-size: 20rpx;
				border-radius: 0 !important;
				background-color: white;
				margin-top: 11rpx;

				.date-1 {
					position: relative;
					padding-bottom: 10rpx;

					&:after {
						content: '-';
						display: block;
						position: absolute;
						bottom: -5rpx;
						left: 0;
						right: 0;
						width: fit-content;
						margin: auto;
					}
				}

				&:first-of-type {
					margin: 0;
				}
			}

			.name {
				border-radius: 0 !important;
				background-color: white;
				color: #303030;
				font-size: 22rpx;
				// margin-top: 4rpx;
			}

			.select-td {
				background-color: #df42d9;
			}

			.select-grey {
				background: #EAEAEA;
			}

			.select-blue {
				background: #59A0FFFF;
			}

			.select-ash {
				background: #EAEAEA;
			}
		}

		.footer {
			padding: 30rpx 30rpx;
			background-color: white;
			position: fixed;
			bottom: 0;
			width: 100%;
			left: 0;

			.footer-1 {
				.info-1 {
					font-size: 28rpx;
					color: #333333;
				}

				.info-2 {
					font-size: 28rpx;
					font-weight: 500;
					color: #ff0000;

					text {
						font-weight: 700;
						font-size: 40rpx;
					}
				}

				.btn {
					width: 35%;
					color: white;
					padding: 15rpx 0;
					font-size: 28rpx;
					text-align: center;
					border-radius: 30px;
					background-color: #dcdcdc;
				}

				.btn_n {
					width: 35%;
					color: white;
					padding: 15rpx 0;
					font-size: 28rpx;
					text-align: center;
					border-radius: 30px;
					background-color: #59A0FFFF;
				}
			}

			.footer-2 {
				margin-bottom: 15rpx;

				.info-1 {
					font-size: 22rpx;
					font-weight: bold;
					color: #008CFF;

					image {
						width: 30rpx;
						height: 30rpx;
						margin-right: 8rpx;
					}
				}

				.info-2 {
					flex: 1;
					margin-left: 60rpx;

					.tag-item {
						font-size: 24rpx;
						font-weight: bold;
						color: #333333;

						.color {
							width: 28rpx;
							height: 28rpx;
							background: #EAEAEA;
							border-radius: 4rpx;
							margin-right: 15rpx;
						}

						.color2 {
							background-color: #6373e8;
						}

						.color3 {
							background-color: #a6ccff;
						}

						.color4 {
							background-color: #df42d9;
						}
					}
				}
			}

			.footer-3 {
				display: flex;
				margin-bottom: 30rpx;

				.item {
					background: #F3F3F3;
					border-radius: 5px;
					flex-shrink: 0;
					width: fit-content;
					margin-top: 20rpx;
					margin-right: 40rpx;
					position: relative;

					.icon {
						position: absolute;
						top: -14rpx;
						right: -14rpx;
					}

					.item-time {
						font-size: 22rpx;
						background: #DF42D9;
						color: #FFFFFF;
						text-align: center;
						padding: 5rpx 0;
						border-radius: 5px 5px 0 0;
					}

					.item-con {
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						padding: 16rpx;
						font-size: 22rpx;
						font-weight: bold;

						.item-con1 {

							color: #333333;
							margin-bottom: 15rpx;
						}

						.item-con2 {
							color: #DF42D9;
						}
					}
				}
			}
		}
	}
</style>
